package com.cindata

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class RiskWarningFlowStageRoleController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond RiskWarningFlowStageRole.list(params), model:[riskWarningFlowStageRoleCount: RiskWarningFlowStageRole.count()]
    }

    def show(RiskWarningFlowStageRole riskWarningFlowStageRole) {
        respond riskWarningFlowStageRole
    }

    def create() 
    {
        params['targetUri'] = request.getHeader("referer")
        respond new RiskWarningFlowStageRole(params)
    }

    @Transactional
    def save(RiskWarningFlowStageRole riskWarningFlowStageRole) {
        if (riskWarningFlowStageRole == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningFlowStageRole.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningFlowStageRole.errors, view:'create'
            return
        }

        riskWarningFlowStageRole.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'riskWarningFlowStageRole.label', default: 'RiskWarningFlowStageRole'), riskWarningFlowStageRole.id])
                redirect url: params['targetUri']
            }
            '*' { respond riskWarningFlowStageRole, [status: CREATED] }
        }
    }

    def edit(RiskWarningFlowStageRole riskWarningFlowStageRole) 
    {
        params['targetUri'] = request.getHeader("referer")
        respond riskWarningFlowStageRole
    }

    @Transactional
    def update(RiskWarningFlowStageRole riskWarningFlowStageRole) 
    {
        if (riskWarningFlowStageRole == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningFlowStageRole.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningFlowStageRole.errors, view:'edit'
            return
        }

        riskWarningFlowStageRole.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'riskWarningFlowStageRole.label', default: 'RiskWarningFlowStageRole'), riskWarningFlowStageRole.id])
                redirect url: params['targetUri']
            }
            '*'{ respond riskWarningFlowStageRole, [status: OK] }
        }
    }

    @Transactional
    def delete(RiskWarningFlowStageRole riskWarningFlowStageRole) {

        if (riskWarningFlowStageRole == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        riskWarningFlowStageRole.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'riskWarningFlowStageRole.label', default: 'RiskWarningFlowStageRole'), riskWarningFlowStageRole.id])
                redirect url: request.getHeader("referer")
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'riskWarningFlowStageRole.label', default: 'RiskWarningFlowStageRole'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
